//
//  HZAreaPickerView.m
//  areapicker
//
//  Created by Cloud Dai on 12-9-9.
//  Copyright (c) 2012年 clouddai.com. All rights reserved.
//

#import "HZAreaPickerView.h"
#import <QuartzCore/QuartzCore.h>
//#import "CGRectMakeRedefine.h"

#define kDuration 0.3

@interface HZAreaPickerView ()
{
    NSArray *provinces, *cities, *areas;
}
@end

@implementation HZAreaPickerView

@synthesize delegate=_delegate;
@synthesize pickerStyle = _pickerStyle;
@synthesize locate = _locate;
@synthesize locatePicker = _locatePicker;



- (id)initWithStyle:(HZAreaPickerStyle)pickerStyle delegate:(id<HZAreaPickerDelegate>)delegate
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"HZAreaPickerView" owner:self options:nil] objectAtIndex:0];
    if (self) {
        self.delegate = delegate;
        self.pickerStyle = pickerStyle;
        self.locatePicker.dataSource = self;
        self.locatePicker.delegate = self;
        
        _locate = [HZLocation sharedManager];
        //加载数据
        if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
            NSString * documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            documentPath = [documentPath stringByAppendingPathComponent:@"area.plist"];
            NSFileManager * fm = [NSFileManager defaultManager];
            if([fm fileExistsAtPath:documentPath]){
                provinces = [[NSArray alloc] initWithContentsOfFile:documentPath];
            }else{
                provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
            }
            
            cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
            
            _locate.state = [[provinces objectAtIndex:0] objectForKey:@"state"];
            _locate.stateID = [[provinces objectAtIndex:0] objectForKey:@"stateID"];
            
            _locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];
            _locate.cityID = [[cities objectAtIndex:0] objectForKey:@"cityID"];
            
            areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
            if (areas.count > 0) {
                
                _locate.district = [[self handleAreasStr:[areas objectAtIndex:0]] objectAtIndex:0];
                _locate.districtID = [[self handleAreasStr:[areas objectAtIndex:0]] objectAtIndex:1];
            } else{
               _locate.district = @"";
                _locate.districtID = @"0";
            }
            
        }
    }
    return self;
}

#pragma mark - PickerView lifecycle
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        return 3;
    } else{
        return 2;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [provinces count];
            break;
        case 1:
            return [cities count];
            break;
        case 2:
            if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
                return [areas count];
                break;
            }
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //if(self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
    switch (component){
        case 0:
            return [[provinces objectAtIndex:row] objectForKey:@"state"];
            break;
        case 1:
            return [[cities objectAtIndex:row] objectForKey:@"city"];
            break;
        case 2:
            if ([areas count] > 0) {
                NSArray *arr = [self handleAreasStr:[areas objectAtIndex:row]];
                return arr[0];
                break;
            }
        default:
            return @"";
            break;
    }
}

-(NSArray *)handleAreasStr:(NSString *)str{
    NSArray *array = [str componentsSeparatedByString:@"_"];
    return array;
}
- (NSMutableArray *)getProvinceIdWithProviceName:(NSString *)ProvinceName andCityIdWithName:(NSString *)CityName andAreaIdWithName:(NSString *)AreaName
{
    _locate = [HZLocation sharedManager];
    NSMutableArray * newArry = [NSMutableArray new];
    NSString *ProvinceIdStr = @"";
    NSString *CityIdStr = @"";
    NSString *AreaIdStr = @"";
    for (NSDictionary *dic in provinces) {
        if ([dic[@"state"] isEqualToString:ProvinceName]) {
            ProvinceIdStr = dic[@"stateID"];
            cities = dic[@"cities"];
            for (NSDictionary * citydic in cities) {
                if ([citydic[@"city"] isEqualToString:CityName]) {
                    CityIdStr = citydic[@"cityID"];
                    areas = citydic[@"areas"];
                    for (int i = 0; i<areas.count; i++) {
                        NSArray *arr = [self handleAreasStr:[areas objectAtIndex:i]];
                        if ([arr[0] isEqualToString:AreaName]) {
                            AreaIdStr = arr[1];
                        }
                    }
                 
                }
            }
            
        }
    }
    [newArry addObjectsFromArray:@[ProvinceIdStr,CityIdStr,AreaIdStr]];
    NSLog(@"%@",newArry);
    return newArry;
    
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        _locate = [HZLocation sharedManager];
        switch (component) {
            case 0:
                cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];
                [self.locatePicker selectRow:0 inComponent:1 animated:YES];
                [self.locatePicker reloadComponent:1];
                
                areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
                [self.locatePicker selectRow:0 inComponent:2 animated:YES];
                [self.locatePicker reloadComponent:2];
                
                _locate.state = [[provinces objectAtIndex:row] objectForKey:@"state"];
                _locate.stateID = [[provinces objectAtIndex:row] objectForKey:@"stateID"];
                
                _locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];
                _locate.cityID = [[cities objectAtIndex:0] objectForKey:@"cityID"];
                if ([areas count] > 0) {
                    NSArray *arr = [self handleAreasStr:[areas objectAtIndex:0]];
                    _locate.district = arr[0];
                    _locate.districtID = arr[1];
                }else{
                    _locate.district = @"";
                    _locate.districtID = @"0";
                }
                break;
            case 1:
                areas = [[cities objectAtIndex:row] objectForKey:@"areas"];
                [self.locatePicker selectRow:0 inComponent:2 animated:YES];
                [self.locatePicker reloadComponent:2];
                
                _locate.city = [[cities objectAtIndex:row] objectForKey:@"city"];
                _locate.cityID = [[cities objectAtIndex:row] objectForKey:@"cityID"];
                
                if ([areas count] > 0) {
                     NSArray *arr = [self handleAreasStr:[areas objectAtIndex:0]];
                    _locate.district = arr[0];
                    _locate.districtID = arr[1];
                }else{
                    _locate.district = @"";
                    _locate.districtID = @"0";
                }
                break;
            case 2:
                if ([areas count] > 0) {
                    NSArray *arr = [self handleAreasStr:[areas objectAtIndex:row]];
                    _locate.district = arr[0];
                    _locate.districtID = arr[1];
                } else{
                    _locate.district = @"";
                    _locate.districtID = @"0";
                }
                break;
            default:
                break;
        }
    }
    
    if([self.delegate respondsToSelector:@selector(pickerDidChaneStatus:)]) {
        [self.delegate pickerDidChaneStatus:self];
    }
}



-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.font = [UIFont systemFontOfSize:15];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
//        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

#pragma mark - animation

- (void)showInView:(UIView *) view
{
    CGRect rect = CGRectMake(0, view.frame.size.height, self.frame.size.width, self.frame.size.height);
//    self.frame = [CGRectMakeRedefine CGRectMakeDefine:rect];
    [view addSubview:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height, view.frame.size.width, self.frame.size.height);
    }];
}

- (void)cancelPicker
{
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.frame = CGRectMake(0, self.frame.origin.y+self.frame.size.height, self.frame.size.width, self.frame.size.height);
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         
                     }];
}
@end
